<?php
require_once "metadata/Country.class.php";
require_once "/controller/CityController.class.php";
class CountryModel
{
    var $db;

    function __construct($db_link)
    {
        $this->db = $db_link;
    }

    function getCountry($id)
    {
        $query = "SELECT * FROM countries WHERE id = '$id'" or die("Error in the consult.." . mysqli_error($this->db));

        //execute the query.
        $result = $this->db->query($query)  or die("Error in the consult.." . mysqli_error($this->db));

        $city_controller = new CityController($this->db);

        while($row = mysqli_fetch_array($result))
        {
            $capital_name = $city_controller->getCityName($row["capital"]);
            $c = new Country($row["id"], $row["name"], $row["population"], $row["car_code"], $capital_name, $row["continentid"]);
            return $c;
        }
    }

    function getAllCountriesOnContinent($continent_id)
    {
        $query = "SELECT id FROM countries WHERE continentid = '$continent_id'" or die("Error in the consult.." . mysqli_error($this->db));

        //execute the query.
        $result = $this->db->query($query)  or die("$continent_id : " . mysqli_error($this->db));

        $countries = array();

        while($row = mysqli_fetch_array($result))
        {
            $country = $this->getCountry($row["id"]);
            $countries[count($countries)] = $country;
        }
        return $countries;
    }
}
?>